// ActionScript file
import Components.TitleWindow.twSaveProduct;
import Components.TitleWindow.twSaveSupplier;

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.Products;
import org.flexlancer.databases.Suppliers;

[Bindable]
private var supplierData:Array = new Array();

[Bindable]
private var dpProductData:Array = new Array();
[Bindable]
private var completesupplierdata:Array = new Array();
private var completeProductData:Array = new Array();

private function init():void{
	try{
			this.completesupplierdata = Suppliers.List();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function initSupplier():void{
	try{

		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function initProduct():void{
	try{
		this.completeProductData = Products.List();
		this.ShowFilterDataProduct();
		this.cmbProductSupplier.selectedItem = null;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowFilterDataProduct():void{
	try{
		this.dpProductData = new Array();
		
		for each ( var singleData:Object in this.completeProductData ){
			this.dpProductData.push(singleData);
		}
		
		var strValue:String = "";
		var i:Number = 0;
		var obj:Object;
		
		if( this.txtProductID != null){
			strValue = this.txtProductID.text;
			for( i = 0 ; i < this.dpProductData.length ; i++){
				obj = this.dpProductData[i];
				
				if ( String(obj.productid).indexOf(strValue) < 0 ){
					this.dpProductData.splice(i,1);
					i --;
				}
			}
		}
		if ( this.txtProductLabel.text != "" ){
			strValue = this.txtProductLabel.text.toLowerCase();
			for ( i = 0 ; i < this.dpProductData.length ; i++ ){
				obj = this.dpProductData[i];
				
				if ( String(obj.title).toLowerCase().indexOf(strValue) < 0 ){
					this.dpProductData.splice(i,1);
					i --;
				}
			}
		}
		if ( this.cmbProductSupplier.selectedItem != null ){
			strValue = this.cmbProductSupplier.selectedItem.suppliername as String;
			//Alert.show(strValue,"Str Value");
			for ( i = 0 ; i < this.dpProductData.length ; i++ ){
				obj = this.dpProductData[i];
				//Alert.show(obj.suppliername,"Supplier Name");
				if ( String(obj.suppliername).indexOf(strValue) < 0 ){
					this.dpProductData.splice(i,1);
					i --;
				}
			}
		}
		
		/* if ( this.cmbCustomerType.selectedItem != null ){
			strValue = this.cmbCustomerType.selectedItem as String;
			for ( i = 0 ; i < this.customerData.length ; i++ ){
				obj = this.customerData[i];
				
				if ( String(obj.customertype).indexOf(strValue) < 0 ){
					this.customerData.splice(i,1);
					i --;
				}
			}
		} */
		
		
		if( this.txtProductStock != null){
			strValue = this.txtProductStock.text;
			for( i = 0 ; i < this.dpProductData.length ; i++){
				obj = this.dpProductData[i];
				
				if ( String(obj.instock).indexOf(strValue) < 0 ){
					this.dpProductData.splice(i,1);
					i --;
				}
			}
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowFilterData():void{
	try{
		this.supplierData = new Array();
		
		for each ( var singleData:Object in this.completesupplierdata ){
			this.supplierData.push(singleData);
		}
		
		var strValue:String = "";
		var i:Number = 0;
		var obj:Object;
		
		if( this.txtSupplierID != null){
			strValue = this.txtSupplierID.text;
			for( i = 0 ; i < this.supplierData.length ; i++){
				obj = this.supplierData[i];
				
				if ( String(obj.supplierid).indexOf(strValue) < 0 ){
					this.supplierData.splice(i,1);
					i --;
				}
			}
		}
		if ( this.txtSupplierName.text != "" ){
			strValue = this.txtSupplierName.text.toLowerCase();
			for ( i = 0 ; i < this.supplierData.length ; i++ ){
				obj = this.supplierData[i];
				
				if ( String(obj.suppliername).toLowerCase().indexOf(strValue) < 0 ){
					this.supplierData.splice(i,1);
					i --;
				}
			}
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}


private function OntxtSupplierName_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnSupplierID_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnProductID_Changed():void{
	try{
		this.ShowFilterDataProduct();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function PerformEditProduct():void{
	try{
		if ( this.dgProducts.selectedItem != null ){
			var editProduct:twSaveProduct = PopUpManager.createPopUp(this,twSaveProduct,true) as twSaveProduct;
			editProduct.addEventListener(FlexEvent.REMOVE,this.OnRemoveProduct);
			editProduct.EditObject = this.dgProducts.selectedItem;
			PopUpManager.centerPopUp(editProduct);			
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnDeleteConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			Products.Delete("productid = "+this.dgProducts.selectedItem.productid);
			
			this.RefreshData();			
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function PerformDeleteProduct():void{
	try{
		if ( this.dgProducts.selectedItem != null ){
			Alert.show("Are you sure to delete \""+this.dgProducts.selectedItem.title+"\" service?",
			"Delete Confirmation",
			Alert.YES|Alert.NO,
			this,
			this.OnDeleteConfirmation);
		}else{
			Alert.show("Please select any product to delete.","No product selected");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnProducts_KeyUp(evt:KeyboardEvent):void{
	try{
		if ( evt.keyCode == Keyboard.DELETE){
			this.PerformDeleteProduct();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function RefreshData():void{
	try{
		this.completeProductData = Products.List();
		this.dgProducts.dataProvider = this.completeProductData;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnRemoveProduct(evt:FlexEvent):void{
	try{
		this.RefreshData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnProductLabel_Changed():void{
	try{
		this.ShowFilterDataProduct();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnSupplier_Change():void{
	try{
		this.ShowFilterDataProduct();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnProductStock_Changed():void{
	try{
		this.ShowFilterDataProduct();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnProductPrice_Changed():void{
	try{
		this.ShowFilterDataProduct();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddNewSupplier():void{
	try{
		var newSupplier:twSaveSupplier = PopUpManager.createPopUp(this,twSaveSupplier,true) as twSaveSupplier;
		PopUpManager.centerPopUp(newSupplier);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddNewProduct():void{
	try{
		var newProduct:twSaveProduct = PopUpManager.createPopUp(this,twSaveProduct,true) as twSaveProduct;
		newProduct.addEventListener(FlexEvent.REMOVE,this.OnRemoveProduct);
		PopUpManager.centerPopUp(newProduct);
	}catch(err:Error){
		Logger.LogE(err);
	}	
}

private function RefreshSupplierData():void{
	try{
		this.completesupplierdata = Suppliers.List();
		this.ShowFilterData();		
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnResetFilterSupplier():void{
	try{
		this.txtSupplierContactInfo.text = "";
		this.txtSupplierID.text = "";
		this.txtSupplierName.text = "";
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnResetFilterProducts():void{
	try{
		this.txtProductID.text = "";
		this.txtProductLabel.text = "";
		this.cmbProductSupplier.selectedItem = null;
		this.txtProductStock.text = "";
		this.txtProductPrice.text = "";
		this.ShowFilterDataProduct();
	}catch(err:Error){
		Logger.LogE(err);
	}
}